package cn.edu.zhku.aspect;

import cn.edu.zhku.dto.BaseDto;
import cn.edu.zhku.util.ShiroUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Order(0)
public class SetUserIdAspect extends BaseDtoAspect {

    @Pointcut("@annotation(cn.edu.zhku.aspect.annotation.SetUserId)")
    public void pointcut() {}

    @Around("baseDtoPointcut() && pointcut()")
    public Object setUserId(ProceedingJoinPoint joinPoint) throws Throwable {
        BaseDto baseDto = (BaseDto) joinPoint.getArgs()[0];
        if (baseDto.getUserId() == null || baseDto.getUserId() == -1)
            baseDto.setUserId(ShiroUtil.getUserId());
        return joinPoint.proceed();
    }

}
